package site.potatoblog.threadpool.manager.util;

import org.springframework.http.HttpStatus;

import java.util.List;

public class Response {
    private int code;
    private String msg;
    private int count;
    private Object data;

    public Response(int code, String msg, int count, Object data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }
    public static Response ok(String msg,Object data){
        int count=1;
        if(data instanceof List){
            count=((List<?>) data).size();
        }
        return new Response(0,msg,count,data);
    }
    public static Response ok(String msg){
        return new Response(0,msg,0,null);
    }
    public static Response error(String msg){
        return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(),msg,0,null);
    }
    public static Response error(String msg,Object data){
        int count=1;
        if(data instanceof List){
            count=((List<?>) data).size();
        }
        return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(),msg,count,data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Object getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", count=" + count +
                ", data=" + data +
                '}';
    }
}
